//
//  JFBAnimation.h
//  com.jufangbian.m
//
//  Created by feynmanyuan on 15/4/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//


#import "JFBUIAnimation.h"
#import "SKBounceAnimation.h"

@implementation JFBUIAnimation
+(void) errorInputTips:(UIView *)view  animation:(TipsAnimation)animation{
    CAKeyframeAnimation *bounce = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
//    CATransform3D forward, back, forward2, back2, forward3, back3, forward4, back4;
    ///设置动画
//    if(animation == TipsBounces) {
//        forward = CATransform3DMakeScale(1.2, 1.2, 1);
//        back = CATransform3DMakeScale(0.8, 0.8, 1);
//        forward2 = CATransform3DMakeScale(1.15, 1.15, 1);
//        back2 = CATransform3DMakeScale(0.85, 0.85, 1);
//        forward3 = CATransform3DMakeScale(1.1, 1.1, 1);
//        back3 = CATransform3DMakeScale(0.9, 0.9, 1);
//        forward4 = CATransform3DMakeScale(1.05, 1.05, 1);
//        back4 = CATransform3DMakeScale(0.95, 0.95, 1);
//    } else {
//        forward = CATransform3DMakeTranslation(-5.0f, 0.0f, 0);
//        back = CATransform3DMakeTranslation(10.0f, 0.0f, 0);
//        forward2 = CATransform3DMakeTranslation(-8.0f, 0.0f, 0);
//        back2 = CATransform3DMakeTranslation(6.0f, 0.0f, 0);
//        forward3 = CATransform3DMakeTranslation(-4.0f, 0.0f, 0);
//        back3 = CATransform3DMakeTranslation(2.0f, 0.0f, 0);
//        forward4 = CATransform3DMakeTranslation(-2.0f, 0.0f, 0);
//        back4 = CATransform3DMakeTranslation(1.0f, 0.0f, 0);
//    }
//    [bounce setValues:[NSArray arrayWithObjects:
//                       [NSValue valueWithCATransform3D:CATransform3DIdentity],
//                       [NSValue valueWithCATransform3D:forward],
//                       [NSValue valueWithCATransform3D:back],
//                       [NSValue valueWithCATransform3D:forward2],
//                       [NSValue valueWithCATransform3D:back2],
//                       [NSValue valueWithCATransform3D:forward3],
//                       [NSValue valueWithCATransform3D:back3],
//                       [NSValue valueWithCATransform3D:forward4],
//                       [NSValue valueWithCATransform3D:back4],
//                       [NSValue valueWithCATransform3D:CATransform3DIdentity],nil]];

    float duration = animation == TipsBounces ? 1.0f : 0.5f;
    [bounce setDuration:duration];
    [view.layer addAnimation:bounce forKey:@"bounceAnimation"];
}

+(void)alertTips:(NSString*)msg {
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:msg
                          delegate:nil
                          cancelButtonTitle:nil
                          otherButtonTitles:@"确定",nil];
    [alert show];
}

+(void)alertTips:(NSString*)msg delegate:(id)delegate {
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:msg
                          delegate:delegate
                          cancelButtonTitle:nil
                          otherButtonTitles:@"确定",nil];
    [alert show];
}

+(void)alertSure:(NSString*)msg delegate:(id)delegate {
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:msg
                          delegate:delegate
                          cancelButtonTitle:@"取消"
                          otherButtonTitles:@"确定",nil];
    [alert show];
}

+(void)playLoadingLayer:(UIView*)view{
    UIImageView *bouncingView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cr"]];
    bouncingView.bounds = CGRectMake(0, 0, 60, 60);
    bouncingView.frame = CGRectMake(view.bounds.size.width / 2 - 30, view.bounds.size.height / 2 - 60, 60, 60);
    bouncingView.backgroundColor = [UIColor clearColor];
    [view addSubview:bouncingView];
    
    SKBounceAnimation *positionAnimation = [SKBounceAnimation animationWithKeyPath:@"position"];
    positionAnimation.fromValue = [NSValue valueWithCGPoint:bouncingView.center];
    CGPoint point = CGPointMake(bouncingView.center.x - 10, bouncingView.center.y - 10);
    positionAnimation.toValue = [NSValue valueWithCGPoint:point];
    positionAnimation.duration = 100.0f;
    positionAnimation.numberOfBounces = 100;
    
    positionAnimation.removedOnCompletion = NO;
    positionAnimation.fillMode = kCAFillModeForwards;
    
    [bouncingView.layer addAnimation:positionAnimation forKey:@"playLoading"];
}

+ (void)imageViewRadis:(UIView*)view radius:(float)radius {
    CALayer * l = [view layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:radius];
}

+(void)topTips:(UIView*)view tipString:(NSString*)tipString delegate:(id)delegate{
    float offsetY = 40.0f; int step = 10;
    
    CAKeyframeAnimation *position = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CAKeyframeAnimation *opacitykeyFrame = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    
    CGSize wordSize = [tipString sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(200, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    
    float blockX = (view.frame.size.width - wordSize.width )/2 - 10;
    float blockY = -wordSize.height - 6;
    
    UIView *tview = [[UIView alloc] initWithFrame:CGRectMake(blockX,  -wordSize.height - 6, wordSize.width + 20, wordSize.height + 6)];
    
    [self imageViewRadis:tview radius:5.0f];
    tview.backgroundColor = ApplicationColor;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 3.0f, wordSize.width, wordSize.height)];
    label.text = tipString;
    label.textColor = [UIColor whiteColor];
    //iOS6需增加此行不然label背景是白色
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:12.0f];
    [tview addSubview:label];
    tview.alpha = 0.0f;
    [view addSubview:tview];
    position.delegate = delegate;
    
    NSMutableArray *posArray = [@[] mutableCopy];
    NSMutableArray *opacityArray = [@[] mutableCopy];
    
    float setpOffset = (wordSize.height + 6 + offsetY) / step;
    
    for( int idx = 0; idx < step * 2 + 1; idx++ ) {
        int f =  step - abs(step - idx);
        int s = 1;
        
        if(idx == step) {
            for(int subIdx = 0; subIdx < 5; subIdx ++) {
                [posArray addObject:[NSValue valueWithCGPoint:CGPointMake(view.frame.size.width / 2, blockY + f*s*setpOffset)]];
                [opacityArray addObject:[NSNumber numberWithFloat: f / (float)step]];
            }
        }
        
        [posArray addObject:[NSValue valueWithCGPoint:CGPointMake(view.frame.size.width / 2, blockY + f*s*setpOffset)]];
        [opacityArray addObject:[NSNumber numberWithFloat: f / (float)step]];
    }
    [opacitykeyFrame setValues:opacityArray];
    [position setValues:posArray];
    
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = [NSArray arrayWithObjects:opacitykeyFrame, position, nil];
    
    animGroup.delegate = delegate;//[XCHelper class];
    animGroup.removedOnCompletion = YES;
    [animGroup setDuration:1.8f];
    [tview.layer addAnimation:animGroup forKey:@"bounceAnimation"];
}

-(void)SVProgressHUDShow{
//    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];
//    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD show];
}

@end




